Imports IICModel.ConversionObjects
Imports IICModel.BusObjects
Imports IICModel.ConversionObjects.Mappers
Imports IICModel.BusObjects.Mappers

Namespace DataConbversionLogic
    Public Class PXSClassesConverter
        Inherits BaseDataConverter

        Protected Overrides Sub mapData(ByVal source As org.model.lib.Model.IModelObject, _
                                        ByRef destination As org.model.lib.Model.IModelObject)

            Dim s As PXS_SCH = CType(source, PXS_SCH)
            Dim d As PeriodClass = CType(destination, PeriodClass)

            Dim pprddb As New PXS_PRDSDBMapper
            pprddb.dbConn = Me.dataConvMapper.dbConn
            Dim pprd As PXS_PRDS = pprddb.findWhere("Code={0}", s.ExamPeriod)
            Dim lperiod As Period = PeriodDataUtils.findOne("diplomasupertypeid=4 and PeriodCode={0}", s.ExamPeriod)
            Dim llesson As Lesson = LessonsCache.current.getLessonByCode("CFA")

            If pprd Is Nothing Then
                Me.logMsg(s.Id & " skipped, PXS_PRDS not found from" & s.ExamPeriod)
                destination = Nothing
                Exit Sub
            End If

            If llesson Is Nothing Then
                Me.logMsg(s.Id & " skipped, Lesson not found from code CFA")
                destination = Nothing
                Exit Sub
            End If
            If lperiod Is Nothing Then
                Me.logMsg(s.Id & " skipped, period not found from code " & s.ExamPeriod)
                destination = Nothing
                Exit Sub
            End If

            Dim classCheck As PeriodClass = PeriodClassDataUtils.findOne( _
                        "Lessonid={0} and periodid={1}", _
                        llesson.Id, lperiod.Id)

            If classCheck IsNot Nothing Then
                Me.logMsg(s.Id & " skipped, already exists ")
                destination = Nothing
                Exit Sub
            End If
            '--------------------------------
            d.ExamDate = s.DateVal
            d.ExamTimeFrom = GetVBTimeFromClarionTime(CInt(s.StartTm), CDate(d.ExamDate))
            d.ExamTimeTo = GetVBTimeFromClarionTime(CInt(s.EndTm), CDate(d.ExamDate))
            d.ExamCenterID = CType(IIf(s.ExamCentre = "01", 1, 2), Integer?)
            '--------------------------------
            d.LessonId = llesson.LessonId
            d.PeriodId = lperiod.PeriodId
            d.DataConversionSourceTable = getTableName()
            d.PassGrade = CType(pprd.PassGrade, Integer?)

        End Sub

        Protected Overrides Function getTableName() As String
            Return "PXS_SCH"
        End Function

        Protected Overrides Sub instantiateMappers()

            Me.dataConvMapper = New PXS_SCHDBMapper()
            Me.newdbMapper = New PeriodClassDBMapper

        End Sub

        Protected Overrides Function checkData(ByVal source As IModelObject, _
                                  ByVal destination As IModelObject) As Boolean

            Return True
        End Function


    End Class
End Namespace